package cn.gailvlun.gll.presentation.chat.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.Member
import cn.gailvlun.gll.net.apartment.room.Room
import cn.gailvlun.gll.util.ProfileUtil
import io.realm.Realm


class RoomDetailViewModel: ViewModel {

    var roomId: Int
    var room: Room?
    var member: Member?
    private var realm: Realm?

    constructor(roomId: Int) : super() {
        this.roomId = roomId

        realm = Realm.getInstance(ChatDB2.getConfiguration())
        room = realm!!.where(Room::class.java)
                .equalTo("id", roomId)
                .findFirst()

        member = realm!!.where(Member::class.java)
                .equalTo("room", roomId)
                .equalTo("owner.id", ProfileUtil.getUserId())
                .findFirst()
    }

    fun updateMember() {
        member = realm!!.where(Member::class.java)
                .equalTo("room", roomId)
                .equalTo("owner.id", ProfileUtil.getUserId())
                .findFirst()
    }

    override fun onCleared() {
        realm?.close()
        realm = null

        super.onCleared()
    }
}